package soft.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import soft.pojo.*;
import soft.service.ErrorGradeService;
import soft.service.GradeService;
import soft.service.TeacherService;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/teacher")
public class TeacherControl {
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private GradeService gradeService;
    @Autowired
    private ErrorGradeService errorGradeService;
    //查询个人信息
    @RequestMapping(value = "/getself")
    public String getSelf(ModelMap modelMap, HttpSession session){
        System.out.println(session.getAttribute("aId"));
        //得强制转化为String
        Teacher bean = teacherService.getById((String) session.getAttribute("aId"));
        modelMap.addAttribute("teachermsg",bean);
        return "selectTeacher";
    }

    //查询到个人信息后更新
    @RequestMapping(value = "/getself1")
    public String getSelf1( ModelMap modelMap,HttpSession session){
        System.out.println(session.getAttribute("aId"));
        //得强制转化为String
        Teacher bean = teacherService.getById((String) session.getAttribute("aId"));
        modelMap.addAttribute("teachermsg",bean);

        return "teacherMsg";
    }
    //更新个人信息
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(Teacher bean, ModelMap modelMap,HttpSession session){
        System.out.println(bean);
        bean.setRegTime(new Date());
        boolean flag =teacherService.updateTeacher(bean);
        if (flag == true){
            return "forward:/teacher/getself";
        }else{
            return "teacherMsg";
        }

    }


    //查询所有成绩信息
    @RequestMapping(value = "/allgrade")
    public String allGrade(ModelMap model,HttpSession session){
        List<Grade> bean = gradeService.getList();
        model.addAttribute("findgrade",bean);
        //     session.setAttribute("allclass",bean);
        return "findAllGrade";
    }
    //模糊查询
    @RequestMapping(value = "/getLikeName")
    public String getLikeName(String sName,ModelMap model) {
        System.out.println("sName:::"+sName);

        List<Grade> bean = gradeService.getLikeName(sName);
        model.addAttribute("findgrade", bean);
        //return "findgrade";
        return "findAllGrade";
    }

    //删除成绩信息
    @RequestMapping(value = "/deletegrade")
    public String deleteGrade(String sId,ModelMap model,HttpSession session){
        System.out.println(sId);
        gradeService.deleteGrade(sId);
        // session.commit();
        List<Grade> bean = gradeService.getList();
        //  session.setAttribute("allclass",bean);
        model.addAttribute("findgrade",bean);
        return "findAllGrade";
    }

    //成绩管理,先跳转到页面，在进行修改，保存
    @RequestMapping(value = "/gotoupdategrade")
    public String gotoUpdateGrade(String sId,String courseName,ModelMap model){

        model.addAttribute("updateonegrade",sId);
        return "updateGrade";
    }

    @RequestMapping(value = "/updategrade")
    public String updateGrade(Grade bean, ModelMap model){
        if(bean.getGrade()>=60){
            bean.setFlag("及格");
        }
        else{
            bean.setFlag("挂科");
        }
        boolean flag = gradeService.update(bean);
        if(flag == true){
            return "forward:/teacher/allgrade";
        }
        else{
            model.addAttribute("msg","操作有误，请重新操作");
            return "error";
        }
    }

    //成绩信息管理,先跳转到页面，在进行添加，保存
    @RequestMapping(value = "/gotoaddgrade")
    public String gotoaddgrade(){
        return "addGrade";
    }

    @RequestMapping(value = "/addgrade")
    public String addGrade(Grade bean, ModelMap model){
        if(bean.getGrade()>=60){
            bean.setFlag("及格");
        }
        else{
            bean.setFlag("挂科");
        }
        boolean flag = gradeService.addGrade(bean);
        if(flag == true){
            return "forward:/teacher/allgrade";
        }
        else{
            model.addAttribute("msg","操作有误，请重新操作");
            return "error";
        }
    }


    //查询所有课程成绩信息
    @RequestMapping(value = "/selectGradeBycourseName")
    public String selectGradeBycourseName(ModelMap model,HttpSession session){
        List<Map> bean = gradeService.selectGradeBycourseName();
        model.addAttribute("selectGradeBycourseName",bean);
        //     session.setAttribute("allclass",bean);
        System.out.println(bean);
        return "selectGradeBycourseName";
    }

    //查询所有课程成绩信息
    @RequestMapping(value = "/selectAvgGrade")
    public String selectAvgGrade(ModelMap model,HttpSession session){
        List<Map> bean = gradeService.selectAvgGrade();
        model.addAttribute("selectAvgGrade",bean);
        //     session.setAttribute("allclass",bean);
        System.out.println(bean);
        return "selectAvgGrade";
    }

    //查询所有成绩信息
    @RequestMapping(value = "/allErrorGrade")
    public String allErrorGrade(ModelMap model,HttpSession session){
        List<ErrorGrade> bean = errorGradeService.getList();
        model.addAttribute("findErrorGrade",bean);
        //     session.setAttribute("allclass",bean);
        return "findErrorGrade";
    }

    //删除成绩信息
    @RequestMapping(value = "/deleteErrorGrade")
    public String deleteErrorGrade(String sId,ModelMap model,HttpSession session){
        errorGradeService.deleteError(sId);
        // session.commit();
        List<ErrorGrade> bean = errorGradeService.getList();
        //  session.setAttribute("allclass",bean);
        model.addAttribute("findErrorGrade",bean);
        return "findErrorGrade";
    }

}
